<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>结算类型</title>
    </head>
    <body>
        <div id="settlementTypesGrid" style="height:580px;" class="x-form-item">
        </div>
        <script>
            /*
             * Ext JS Library 2.0.2
             * Copyright(c) 2006-2008, Ext JS, LLC.
             * licensing@extjs.com
             *
             * http://extjs.com/license
             */
            Ext.onReady(function(){
                var settlementTypeCM = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
                    header: '编号',
                    dataIndex: 'id'
                }, {
                    header: '结算类型',
                    dataIndex: 'settlement_type'
                }]);
                //隐藏系统编号
                settlementTypeCM.setHidden(1, !settlementTypeCM.isHidden(1));
                var settlementTypeDS = new Ext.data.Store({
                    proxy: new Ext.data.HttpProxy({
                        url: '/admin/getAllSettlementTypes'
                    }),
                    reader: new Ext.data.JsonReader({
                        totalProperty: 'totalProperty',
                        root: 'root'
                    }, [{
                        name: 'id'
                    }, {
                        name: 'settlement_type'
                    }])
                });
                /************************************************************
                 * Define menubar now in here
                 *   add and delete functions
                 ************************************************************/
                var menubar = [{
                    text: '增加结算类型',
                    tooltip: '增加结算类型',
                    iconCls: 'addItem',
                    handler: function(){
                        // add new settlementtype now - action
                        CreateSettlementType(settlementTypeDS);
                    }
                }, '-', {
                    text: '删除结算类型',
                    tooltip: '删除结算类型',
                    iconCls: 'remove',
                    handler: function(){
                        // delete settlementtype now - action
                        DeleteSettlementType(settlementTypeDS, settlementTypeGridPanel);
                    }
                }, '-', {
                    text: '编辑结算类型',
                    tooltip: '编辑结算类型',
                    iconCls: 'editItem',
                    handler: function(){
                        // delete settlementtype now - action
                        EditSettlementType(settlementTypeDS, settlementTypeGridPanel);
                    }
                }];
                var settlementTypeGridPanel = new Ext.grid.GridPanel({
                    el: 'settlementTypesGrid',
                    ds: settlementTypeDS,
                    cm: settlementTypeCM,
                    //sm: sm,
                    collapsible: true,
                    animCollapse: false,
                    title:'结算类型列表',
                    tbar: menubar,
                    loadMask: true
                });
                settlementTypeGridPanel.render();
                //ds.load();
                settlementTypeDS.load();
                
            });
            //---------------------Create SettlementType------------------------
            function CreateSettlementType(dataStore){
            
                // pre-define fields in the form
                var settlementtype = new Ext.form.TextField({
                    fieldLabel: '结算类型',
                    allowBlank: false,
                    name: 'settlement_type',
                    anchor: '90%'
                });
                
                var formPanel = new Ext.form.FormPanel({
                    baseCls: 'x-plain',
                    labelWidth: 75,
                    url: '/admin/addSettlementType',
                    defaultType: 'textfield',
                    items: [settlementtype]
                });
                
                // define window and show it in desktop
                var window = new Ext.Window({
                    title: '增加结算类型',
                    width: 300,
                    height: 150,
                    minWidth: 300,
                    minHeight: 150,
                    layout: 'fit',
                    plain: true,
                    bodyStyle: 'padding:5px;',
                    buttonAlign: 'center',
                    items: formPanel,
                    modal: true,
                    buttons: [{
                        text: '增加',
                        handler: function(){
                            // check form value 
                            if (formPanel.form.isValid()) {
                                formPanel.form.submit({
                                    waitMsg: 'In processing',
                                    success: function(form, action){
                                        Ext.MessageBox.alert('提示', '提交成功');
                                        window.destroy();
                                        dataStore.load();
                                    },
                                    failure: function(form, action){
                                        Ext.MessageBox.alert('提交错误消息', '提交失败');
                                    }
                                });
                            }
                            else {
                                Ext.MessageBox.alert('错误提示', '请按要求填写必输选项.');
                            }
                        }
                    }, {
                        text: '取消',
                        handler: function(){
                            window.destroy();
                        }
                    }]
                });
                
                window.show();
            };
            //----------------Delete SettlementType--------------------------
            function DeleteSettlementType(dataStore, gridPanel){
                // get the selected items
                var m = gridPanel.getSelections();
                if (m.length > 0) {
                    Ext.MessageBox.confirm('提示', '你真的要删除这些结算类型吗？', function(btn){
                        if (btn == 'yes') {
                            var jsonData = "[";
                            for (var i = 0, len = m.length; i < len; i++) {
                                var ss = "{\"id\":\"" + m[i].get("id") + "\"}";
                                if (i == 0) 
                                    jsonData = jsonData + ss;
                                else jsonData = jsonData + "," + ss;
                                dataStore.remove(m[i]);
                            }
                            jsonData = jsonData + "]";
                            //Ext.MessageBox.alert(jsonData);
                            dataStore.load({
                                params: {
                                    delData: jsonData
                                }
                            });
                        }
                    });
                }
                else {
                    Ext.MessageBox.alert('错误提示', '要删除结算类型时，请至少选择一个结算类型');
                }
            };
            
            //------------Edit SettlementType-----------------
            function EditSettlementType(dataStore, gridPanel){//selectedId
                // pre-define fields in the form
                //Ext.MessageBox.alert('提示',selectedId);
                var m = gridPanel.getSelections();
                if (m.length > 0 && m.length == 1) {
                    var selectedId = m[0].get("id");
                    var settlementtype = new Ext.form.TextField({
                        fieldLabel: '结算类型',
                        allowBlank: false,
                        name: 'settlement_type',
                        anchor: '90%'
                    });
                    
                    // create form panel
                    var editFormPanel = new Ext.form.FormPanel({
                        baseCls: 'x-plain',
                        labelWidth: 75,
                        url: '/admin/editSettlementType?id=' + selectedId,
                        reader: new Ext.data.JsonReader({
                            root: 'root'
                        }, ['id', 'settlement_type']),
                        
                        items: [settlementtype]
                    });
                    
                    // load form and assign value to fields
                    editFormPanel.form.load({
                        url: '/admin/getSettlementTypeByID?id=' + selectedId,
                        waitMsg: '正在载入数据...',
                        success: function(form, action){
                            //Ext.MessageBox.alert('提示信息', '信息加载成功！');
                        },
                        failure: function(form, action){
                            Ext.MessageBox.alert('提示信息', '信息加载失败');
                        }
                    });
                    
                    // define window and show it in desktop
                    var editWindow = new Ext.Window({
                        title: '编辑结算类型',
                        width: 300,
                        height: 150,
                        minWidth: 300,
                        minHeight: 150,
                        layout: 'fit',
                        plain: true,
                        bodyStyle: 'padding:5px;',
                        buttonAlign: 'center',
                        items: editFormPanel,
                        modal: true,
                        buttons: [{
                            text: '提交',
                            handler: function(){
                                // check form value 
                                if (editFormPanel.form.isValid()) {
                                    editFormPanel.form.submit({
                                        params: {
                                            id: selectedId
                                        },
                                        waitMsg: '处理过程中...',
                                        failure: function(form, action){
                                            //debugger;
                                            Ext.MessageBox.alert('错误提示', action.failureType);
                                        },
                                        success: function(form, action){
                                            Ext.MessageBox.alert('确认', '提交成功');
                                            editWindow.hide();
                                            dataStore.load();
                                        }
                                    });
                                }
                                else {
                                    Ext.MessageBox.alert('错误提示', '请按要求填写必输选项.');
                                }
                            }
                        }, {
                            text: '取消',
                            handler: function(){
                                editWindow.hide();
                            }
                        }]
                    });
                    editWindow.show();
                }
                else {
                    Ext.MessageBox.alert('提示', "请先选择一条要编辑记录");
                }
            };
        </script>
    </body>
</html>
